//
//  TrainBeenChangedController.swift
//  AirTravel
//
//  Created by apple on 2021/8/30.
//

import UIKit
import FWPopupView

class TrainBeenChangedController: UIViewController {

    @IBOutlet weak var topView: UIView!
    @IBOutlet weak var backView: UIView!
    @IBOutlet weak var submitBtn: UIButton!
    /// 出发日期
    @IBOutlet weak var departureLab: UILabel!
    /// 到达站
    @IBOutlet weak var arrivalStationLab: UILabel!
    
    /// 日期
    @IBOutlet weak var departureBtn: UIButton!
    /// 到达站
    @IBOutlet weak var arriveBtn: UIButton!
    
    var dataStr = ""
    var according = ""
    var difference = "今天"
    /// 出发站名称
    var fromStationName = ""
    /// 到达站名称
    var toStationName = ""
    /// 0选车，，1改签
    var type = 0
    
    lazy var switchAddressView: SwitchAddressView = {
        let customPopupView = SwitchAddressView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT * 0.6))
        customPopupView.delegate = self
        customPopupView.layer.cornerRadius = 5
        customPopupView.layer.masksToBounds = true
        let vProperty = FWPopupViewProperty()
        vProperty.popupCustomAlignment = .bottomCenter
        vProperty.backgroundColor = UIColor.clear
        vProperty.popupAnimationType = .frame
        vProperty.maskViewColor = UIColor(white: 0, alpha: 0.5)
        vProperty.touchWildToHide = "1"
        vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        vProperty.animationDuration = 0.3
        customPopupView.vProperty = vProperty
    
        return customPopupView
    }()
    
    lazy var radioTimeView: RadioTimeView = {
        let customPopupView = RadioTimeView(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: 450))
        customPopupView.delegate = self
        customPopupView.layer.cornerRadius = 5
        customPopupView.layer.masksToBounds = true
        let vProperty = FWPopupViewProperty()
        vProperty.popupCustomAlignment = .bottomCenter
        vProperty.backgroundColor = UIColor.clear
        vProperty.popupAnimationType = .frame
        vProperty.maskViewColor = UIColor(white: 0, alpha: 0.5)
        vProperty.touchWildToHide = "1"
        vProperty.popupViewEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        vProperty.animationDuration = 0.3
        customPopupView.vProperty = vProperty
    
        return customPopupView
    }()
    
    //视图将要显示
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.isNavigationBarHidden = false
        //设置导航栏背景透明
        self.navigationController?.navigationBar.setBackgroundImage(UIImage(),
                                                                    for: .default)
        self.navigationController?.navigationBar.shadowImage = UIImage()
    }
         
    //视图将要消失
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        //重置导航栏背景
        self.navigationController?.navigationBar.setBackgroundImage(nil, for: .default)
        self.navigationController?.navigationBar.shadowImage = nil
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.title = "改签/变更到站"
        self.topView.layer.cornerRadius = 6
        self.topView.layer.masksToBounds = true
        self.backView.layer.cornerRadius = 6
        self.backView.layer.masksToBounds = true
        self.submitBtn.layer.cornerRadius = 6
        self.submitBtn.layer.masksToBounds = true
        
        self.arrivalStationLab.text = self.toStationName
        self.departureLab.text = self.according + " \(self.difference)"
    }

    
    @IBAction func submit(_ sender: UIButton) {
        let vc = ListTrainController()
        vc.according = self.according
        vc.difference = self.difference
        vc.dataStr = self.dataStr
        vc.fromStationName = self.fromStationName
        vc.toStationName = self.toStationName
        vc.type = 1
        self.navigationController?.pushViewController(vc, animated: true)
    }
    //到达站
    @IBAction func arrival(_ sender: Any) {
        self.switchAddressView.hcp_positioning(city: self.toStationName, logoNumber: 1)
        self.switchAddressView.show()
    }
    //出发日期
    @IBAction func departure(_ sender: Any) {
        self.radioTimeView.show()
    }
    
}

extension TrainBeenChangedController:SwitchAddressDelegate{
    func switchAddressClick(data: Dictionary<String, Any>, type: Int) {
        if type == 2 {
//            self.startCity = "\(data["stationName"] ?? "")"
        }else if type == 3 {
            self.toStationName = "\(data["stationName"] ?? "")"
        }
        self.switchAddressView.hide()
        self.switchAddressView.hideKeyboard()
        self.arrivalStationLab.text = self.toStationName
    }
    
}

extension TrainBeenChangedController:RadioTimeDelegate{
    func radioTimeClick(date: String, according: String, difference: String) {
        self.dataStr = date
        self.according = according
        self.difference = difference
        self.departureLab.text = self.according + " \(self.difference)"
    }
    
    
}
